package com.quectel.cms.controller.sys;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.sys.module.sys.dto.SysConfigDto;
import com.quectel.sys.module.sys.service.SysConfigService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-04-18 09:43:48
 */
@RestController
@RequestMapping("sysConfig")
@Tag(name = "SysConfigController", description = "系统参数相关api")
public class SysConfigController extends BaseController {

    @DubboReference
    private SysConfigService sysConfigService;

    /**
     * 信息
     */
    @GetMapping("queryByConfigKey")
    @Operation(summary = "根据系统键查询系统参数")
    public Response<Object> queryByConfigKey(@RequestParam String configKey) {
        String content = sysConfigService.getConfigValueByKey(configKey);
        return Response.ok().wrap(content);
    }

    /**
     * 系统参数分页查询
     */
    @GetMapping("list")
    @RequirePermissions(values = "sysConfig:list")
    @Operation(summary = "系统参数分页查询")
    public Response<Page<SysConfigDto>> list(@RequestParam Map<String, Object> params) {
        Request request = Request.configParams(params).like("configKey");

        List<SysConfigDto> list = new ArrayList<>();
        int total = sysConfigService.queryTotal(params);
        if (total > 0) {
            list = sysConfigService.queryList(params);
        }
        Page<SysConfigDto> page = new Page(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<SysConfigDto>>ok().wrap(page);
    }


    /**
     * 根据Id查询系统参数
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "sysConfig:info")
    @Operation(summary = "根据Id查询系统参数")
    public Response<SysConfigDto> info(@PathVariable("id") Long id) {

        SysConfigDto sysConfigDto = sysConfigService.selectById(id);

        return Response.<SysConfigDto>ok().wrap(sysConfigDto);
    }

    /**
     * 保存系统参数
     */
    @PostMapping("save")
    @RequirePermissions(values = "sysConfig:edit")
    @Operation(summary = "保存系统参数")
    public Response<Object> save(@RequestBody @Validated SysConfigDto sysConfigDto) {

        fillSaveCommonData(sysConfigDto);

        sysConfigService.save(sysConfigDto);

        return Response.ok();
    }

    /**
     * 更新系统参数
     */
    @PostMapping("update")
    @RequirePermissions(values = "sysConfig:edit")
    @Operation(summary = "更新系统参数")
    public Response<Object> update(@RequestBody @Validated SysConfigDto sysConfigDto) {

        fillEditCommonData(sysConfigDto);

        sysConfigService.updateById(sysConfigDto);

        return Response.ok();
    }

    /**
     * 批量删除系统参数
     */
    @PostMapping("delete")
    @RequirePermissions(values = "sysConfig:delete")
    @Operation(summary = "批量删除系统参数")
    public Response<Object> delete(@RequestBody Long[] ids) {

        sysConfigService.deleteBatch(ids);

        return Response.ok();
    }
}
